package UDP;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * @author 宋鑫宇
 * @version 1.0
 * --------接收端------
 */
public class ServiceDemo2 {
    public static void main(String[] args) throws Exception {
        //1.创建接收端对象,定义端口号为8888
        DatagramSocket socket = new DatagramSocket(8888);

        //2.创建一个接受数据包对象
        byte[] buf = new byte[1024 * 64];
        DatagramPacket packet = new DatagramPacket(buf,buf.length);

        //3.接受数据
        socket.receive(packet);

        //4.取出数据
        int len = packet.getLength();
        String s = new String(buf,0,len);
        System.out.println(s);

        //获取发送端的ip和端口
        System.out.println(packet.getSocketAddress());
        System.out.println(packet.getPort());

        //5.数据接收完毕关闭管道释放资源
        socket.close();
    }
}
